package com.yunbiji.demo.service.impl;

import com.yunbiji.demo.dao.CnNoteDao;
import com.yunbiji.demo.dao.CnShareDao;
import com.yunbiji.demo.entity.CnNote;
import com.yunbiji.demo.entity.CnShare;
import com.yunbiji.demo.service.CnShareService;
import com.yunbiji.demo.util.NoteResult;
import com.yunbiji.demo.util.NoteUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (CnShare)表服务实现类
 *
 * @author makejava
 * @since 2021-06-02 08:38:33
 */
@Service("cnShareService")
public class CnShareServiceImpl implements CnShareService {
    @Resource
    private CnShareDao cnShareDao;
    @Resource
    private CnNoteDao cnNoteDao;

    /**
     * 通过ID查询单条数据
     *
     * @param cnShareId 主键
     * @return 实例对象
     */
    @Override
    public CnShare queryById(String cnShareId) {
        return this.cnShareDao.queryById(cnShareId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<CnShare> queryAllByLimit(int offset, int limit) {
        return this.cnShareDao.queryAllByLimit(offset, limit);
    }

    /**
     * 修改数据
     *
     * @param cnShare 实例对象
     * @return 实例对象
     */
    @Override
    public CnShare update(CnShare cnShare) {
        this.cnShareDao.update(cnShare);
        return this.queryById(cnShare.getCnShareId());
    }

    /**
     * 通过主键删除数据
     *
     * @param cnShareId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String cnShareId) {
        return this.cnShareDao.deleteById(cnShareId) > 0;
    }

    //分享功能(实际上为增加内容)
    public NoteResult<Object> shareNote(String noteId) {
        //向cn_share表中插入记录
        CnShare share = new CnShare();
        String shareId = NoteUtil.createId();
        share.setCnShareId(shareId);//主键
        share.setCnNoteId(noteId);
        //获取笔记标题和内容
        CnNote note=cnNoteDao.queryById(noteId);
        share.setCnShareTitle(note.getCnNoteTitle());
        share.setCnShareBody(note.getCnNoteBody());
        //保存分享记录
        cnShareDao.share(share);
//		//模拟异常
//		String str=null;
//		str.length();
        //构建result
        NoteResult<Object> result = new NoteResult<Object>();
        result.setStatus(0);
        result.setMsg("分享笔记成功");
        return result;
    }
    //搜索功能
    public NoteResult<List<CnShare>> searchNote(String keyword) {
        String title="%"+keyword+"%";
        //模糊查询
        List<CnShare> shares = cnShareDao.findLikeTitle(title);
        //构建返回结果
        NoteResult<List<CnShare>> result = new NoteResult<List<CnShare>>();
        result.setStatus(0);
        result.setMsg("搜索成功");
        result.setData(shares);
        return result;
    }
    //点击搜索后的收藏笔记，从而查看笔记信息
    public NoteResult<CnShare> loadShareNote(String shareId) {
        CnShare share = cnShareDao.queryById(shareId);
        NoteResult result = new NoteResult();
        result.setStatus(0);
        result.setMsg("加载笔记成功");
        result.setData(share);
        return result;
    }

}